import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from "@/views/HomeView"


Vue.use(VueRouter)

const routes = [
    {
        path: "/",
        redirect: "/dashboard",
        component: HomeView,
        meta: {
            requiresAuth: true
        },
        children: [
            {
                path: "/dashboard",
                component: () => import("../views/dashboard")
            },
            {
                path: "/community/index",
                component: () => import("../views/community/index")
            },
            {
                path: "/house/index",
                component: () => import("../views/house/index")
            },
            {
                path: "/house/building",
                component: () => import("../views/house/building")
            },
            {
                path: "/owners/index",
                component: () => import("../views/owners/index")
            },
            {
                path: "/owners/vehicle",
                component: () => import("../views/owners/vehicle")
            },
            {
                path: "/owners/pet",
                component: () => import("../views/owners/pet")
            },
            {
                path: "/parking/index",
                component: () => import("../views/parking/index")
            },
            {
                path: "/parking/usage",
                component: () => import("../views/parking/usage")
            },
            {
                path: "/service/activity",
                component: () => import("../views/service/activity")
            },
            {
                path: "/service/repair",
                component: () => import("../views/service/repair")
            },
            {
                path: "/service/complain",
                component: () => import("../views/service/complain")
            },
            {
                path: "/service/mailbox",
                component: () => import("../views/service/mailbox")
            },
            {
                path: "/device/index",
                component: () => import("../views/device/index")
            },
            {
                path: "/expenses/index",
                component: () => import("../views/expenses/index")
            },
            {
                path: "/expenses/project",
                component: () => import("../views/expenses/project")
            },
            {
                path: "/duty/index",
                component: () => import("../views/duty/index")
            },
            {
                path: "/auth/admin",
                component: () => import("../views/auth/admin")
            },
            {
                path: "/auth/adminlog",
                component: () => import("../views/auth/adminlog")
            },
            {
                path: "/auth/rule",
                component: () => import("../views/auth/rule")
            }
        ]
    },
    {
        path: "/login",
        component: () => import("../views/login")
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    let jwt = localStorage.getItem("jwt");
    if (to.matched.some(record => record.meta.requiresAuth) && !jwt) {
        alert("未登录，请先登录！");
        next("/login"); // 如果用户未登录，则跳转到登录页面
    } else {
        next();
    }
});

export default router
